/**  
 * @Title: RankController.java
 * @Package com.limit.control.admin
 * @author maoyun(maoyun0903@163.com)
 * @date 2014-3-31 下午02:07:51
 * @version V1.0
 */ 
package com.limit.control.admin;

import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Page;
import com.limit.control.admin.validate.RankValidator;
import com.limit.model.Rank;
import com.limit.model.RankGame;

/** 
 * 游戏排行榜<br/>
 * @author maoyun(maoyun0903@163.com)
 * @date 2014-3-31 下午02:07:51 
 * @version V1.0
 */

public class RankController extends BaseController{

	/**
	 * 列表
	 */
	public void index(){
		
		this.setAttr("ranks", Rank.dao.getAllList());
		this.render("ranks.html");
		
	}
	
	public void view(){
		this._commonSetId();
		Rank rank = new Rank();
		if(id > 0){
			rank = Rank.dao.findById(id);
		}
		this.setAttr("rank", rank);
		this.render("rank_view.html");
	}
	
	/**
	 * 添加
	 */
	@Before(RankValidator.class)
	public void save(){
		this.getModel(Rank.class).save();
		this.index();
	}
	
	/**
	 * 修改
	 */
	@Before(RankValidator.class)
	public void update(){
		this.getModel(Rank.class).update();
		this.index();
	}
	
	/**
	 * 删除
	 */
	public void delete(){
		this._commonSetId();
		Rank.dao.findById(id).delete();
		this.index();
		
	}
	
	
	/**
	 * 管理排行榜下所有子成员(游戏)
	 */
	public void members(){
		this._commonSetId();
		int pageNow = this.getParaToInt("p",1);
		// 联合查询
		Page<Rank> members = Rank.dao.complexQuery(id,pageNow,pageSize);
		this.setAttr("members", members);
		this.setAttr("rank_id", id);
		this.render("rank_members.html");
	}
	
	public void rankDel(){
		long rank_id = this.getParaToLong("rank_id", 0L);
		long game_id = this.getParaToLong("game_id", 0L);
		if(rank_id <=0 || game_id<=0){
			this.render("error_param.html");
			return ;
		}
		RankGame.dao.deleteByPrimary(rank_id , game_id);
		id = rank_id ; 
		this.members();
	}
	
	public void sort(){
		long rank_id = this.getParaToLong("rank_id", 0L);
		long game_id = this.getParaToLong("game_id", 0L);
		int  sort	= this.getParaToInt("sort_id", -1);
		RankGame.dao.updateByPrimary(rank_id,game_id,sort);
		id = rank_id ; 
		this.members();
		
	}
	
}
